package com.example.demo.test;

public class IndexLock {

    protected Object[] historyValueArray = new Object[32];
    protected Object[][] valueArray;
    protected volatile int version = 0;
    protected int maxIndex;
    protected int maxCapacity;
    protected int tableIndex;
    protected int nodeIndex;

    public IndexLock() {
        this(8);
    }

    /**
     * 构造函数
     *
     * @param initCapacity 默认初始容量
     */
    public IndexLock(int initCapacity) {
        maxIndex = initCapacity - 1;
        valueArray = new Object[initCapacity][];
        maxCapacity = Integer.MAX_VALUE;
    }

    /**
     * 构造函数
     *
     * @param initCapacity     初始容量
     * @param maxExpansionTime 最大扩容次数
     */
    public IndexLock(int initCapacity, int maxExpansionTime) {
        maxIndex = initCapacity - 1;
        valueArray = new Object[initCapacity][];
        maxCapacity = initCapacity << maxExpansionTime;
    }

    /**
     * 获取指定索引位置的值
     *
     * @param version    版本
     * @param tableIndex 表索引
     * @param nodeIndex  节点索引
     * @return
     */
    public Object get(int version, int tableIndex, int nodeIndex) {
        if (version < this.version) {
            return get((Object[][]) historyValueArray[version], tableIndex, nodeIndex);
        }
        return get(valueArray, tableIndex, nodeIndex);
    }

    protected Object get(Object[][] valueArray, int tableIndex, int nodeIndex) {
        return valueArray[tableIndex][nodeIndex];
    }

    /**
     * 删除指定索引位置的值
     *
     * @param version    版本
     * @param tableIndex 表索引
     * @param nodeIndex  节点索引
     * @return
     */
    public Object remove(int version, int tableIndex, int nodeIndex) {
        if (version < this.version) {
            return remove((Object[][]) historyValueArray[version], tableIndex, nodeIndex);
        }
        return remove(valueArray, tableIndex, nodeIndex);
    }

    protected Object remove(Object[][] valueArray, int tableIndex, int nodeIndex) {
        Object o = valueArray[tableIndex][nodeIndex];
        valueArray[tableIndex][nodeIndex] = null;
        return o;
    }

    public int[] add(Object value) {

        ensureCapacity();

      //  maxIndex-nodeIndex

        if (nodeIndex == maxIndex) {
            nodeIndex = 0;
            tableIndex++;
        }



        valueArray[tableIndex][nodeIndex] = value;

        return new int[]{version, tableIndex, nodeIndex};
    }

    public void set(int version, int tableIndex, int nodeIndex, Object value) {
        if (version < this.version) {
            ((Object[][]) historyValueArray[version])[tableIndex][nodeIndex] = value;
        }
        valueArray[tableIndex][nodeIndex] = value;
    }

    protected void ensureCapacity() {

        if (tableIndex==maxIndex && nodeIndex==maxIndex){

            if (tableIndex==maxCapacity){
               //循环
                while (true){

                }
            }
        }

        if (tableIndex < 0) {
            throw new IndexOutOfBoundsException("tableIndex:" + tableIndex);
        }
        if (tableIndex > maxIndex) {
            //扩容
            historyValueArray[version] = valueArray;




            int c = (maxIndex + 1) << 1;
            Object[][] newValueArray = new Object[c][c];



            version++;

        }
    }
}
